home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
BUS
/
TMCM Software and Labs.sit
/
Software for TMCM 7_95
/
Files for Lab 5
/
3N+1 Sequence
next >
Wrap
Text File
|
1994-04-16
|
2KB
|
55 lines
; This file contains an assembly language program that
; computes the "3N+1" sequence starting from any integer
; N. This sequence is generated by applying the rule:
; IF N is even
; THEN replace N with N divided by 2
; ELSE replace N with 3 times N plus 1
; over and over until N becomes equal to 1. There are
; some possible problems: N might become too big to be
; represented by a 16-bit number. Or, it might be that
; N never becomes 1, so that the sequence never ends.
; These problems are simply ignored in this version of
; the program.
@PC 100 ; This tells the assemble: Put the number 100 into
; the PC register when this program is loaded.
; (This is a special assembler command, and PC is the
; only register that can be used in this way.)
@100 ; This tells the assembler: Put the next instruction
; in location 100. (Usually, it starts at location 0.)
NextN : LOD N ; Get the number N.
SUB-C 1 ; If it is 1, we are done.
JMZ Done
LOD N ; Otherwise, test if N is odd or even.
SHR ; (If it is even, the last bit is zero; if it
JMF Odd ; is odd, that bit is one. The shift-right
; operation drops this bit into the flag
; register, where it can be tested. At the
; same time, the value remaining in the AC
; is N/2.)
Even: STO N ; Number in the AC is already equal to N / 2.
JMP NextN ; Replace N with this number and jump back to
; the start of the loop.
Odd: LOD N ; Compute 3 times N plus 1, replace N with
ADD N ; the result and return to start.
ADD N
ADD-C 1
STO N
JMP NextN
Done: HLT
N: 7 ; The memory location named "N" is loaded
; with the number "7" rather than with an
; assembly language instruction. The initial
; value of N when the program is run is the
; starting point for the "3N+1" sequence.
; Put a different value into N before
; loading the program to start with a different
; value. (In this program, without any
; modifications, N is 115.)